package com.yangguang.huahui.ui.activitys;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.yangguang.huahui.R;
import com.yangguang.huahui.easemob.AppHelper;
import com.yangguang.huahui.entity.Area;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.FlowersType;
import com.yangguang.huahui.entity.HttpResult;
import com.yangguang.huahui.entity.SelectData;
import com.yangguang.huahui.map.GaoLocUtils;
import com.yangguang.huahui.mvp.model.CommonModel;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.CryptoUtil;
import com.yangguang.huahui.utils.GsonUtil;
import com.yangguang.huahui.utils.LogUtil;
import com.yangguang.huahui.utils.ScreenUtils;
import com.yangguang.huahui.utils.SharedPreferenceUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.ResponseBody;
import rx.Subscription;

/**
 * Created by Administrator on 2017/2/8.
 */

public abstract class BaseActivity extends MPermissionsActivity {
    protected Context mContext;
    protected Unbinder mUnbinder;

    protected abstract int initLayoutId();

    protected abstract void initView();

    protected abstract void initData();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initLayoutId();
        super.onCreate(savedInstanceState);
        setContentView(initLayoutId());
        mContext = this;
        mUnbinder = ButterKnife.bind(this);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明状态栏
//            View decorView = getWindow().getDecorView();
//            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//            decorView.setSystemUiVisibility(option);
//            getWindow().setStatusBarColor(Color.TRANSPARENT);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 全透明状态栏
//            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
//            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
//        }

        initData();

        initView();

        DataInfo.TOKEN = SharedPreferenceUtil.getInstance(mContext).getString(SharedPreferenceUtil.TOKEN);
        if (!AppHelper.getInstance().isLoggedIn()) {
            if (!CommonUtil.isNull(DataInfo.TOKEN)) {
                loginEasemob(DataInfo.TOKEN, CryptoUtil.md5(SharedPreferenceUtil.getInstance(this).getString(SharedPreferenceUtil.PASSWORD) + "userKey"));
            }
        }
        DataInfo.NICKNAME = SharedPreferenceUtil.getInstance(mContext).getString(SharedPreferenceUtil.NICKNAME);
        DataInfo.AVATARS = SharedPreferenceUtil.getInstance(mContext).getString(SharedPreferenceUtil.AVATARS);
        LogUtil.e(DataInfo.AVATARS, true);
        new GaoLocUtils(this);
    }


    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    public AlertDialog alertDialog;

    @SuppressLint("NewApi")
    public AlertDialog show_bottom_dialog(View contentView) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        //放在window的setContentView之前
        alertDialog.show();
        window.setContentView(contentView);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager windowManager = window.getWindowManager();
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = (int) (metrics.widthPixels); //设置宽度
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.main_menu_animstyle);
        alertDialog.setCanceledOnTouchOutside(true);
        return alertDialog;
    }

    @SuppressLint("NewApi")
    public void show_dialog_max(View contentView, Activity activity) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.dialog);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        //放在window的setContentView之前
        alertDialog.show();
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = ScreenUtils.getScreenWidth(activity);
        params.height = ScreenUtils.getScreenHeight(activity);
        window.setContentView(contentView, params);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.TOP);
        window.setWindowAnimations(R.style.main_menu_animstyle);
        alertDialog.setCanceledOnTouchOutside(true);
    }

    @SuppressLint("NewApi")
    public AlertDialog show_dialog_max(View contentView) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        //放在window的setContentView之前
        alertDialog.show();
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = ScreenUtils.getScreenWidth(mContext);
        params.height = ScreenUtils.getScreenHeight(mContext);
        window.setContentView(contentView, params);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.TOP);
        window.setWindowAnimations(R.style.main_menu_animstyle);
        alertDialog.setCanceledOnTouchOutside(true);
        return alertDialog;
    }

    public List<SelectData.ListBean> identity_datas = new ArrayList<>();

    /**
     * 获取选择的数据
     * @param type 1身份类型 2规格类 3服务方式
     */
    public void getSelectData(int type) {
        RxManager.getInstance().doSubscribe(new CommonModel().getSelectData(type), new RxSubscriber<SelectData>(false) {
            @Override
            protected void _onNext(SelectData selectData) {
                identity_datas = selectData.getList();
            }

            @Override
            protected void _onError() {

            }
        });
    }


    public Area mArea;

    public void getArea() {
        String result = SharedPreferenceUtil.getInstance(mContext).getString(SharedPreferenceUtil.AREANEW);
        if (!CommonUtil.isNull(result)) {
            mArea = GsonUtil.parseJsonWithGson(result, Area.class);
        } else {
            RxManager.getInstance().doSubscribe(new CommonModel().getArea(), new RxSubscriber<ResponseBody>(false) {
                @Override
                protected void _onNext(ResponseBody responseBody) {
                    try {
                        SharedPreferenceUtil.getInstance(mContext).putString(SharedPreferenceUtil.AREANEW, responseBody.string());
                        mArea = GsonUtil.parseJsonWithGson(SharedPreferenceUtil.getInstance(mContext).getString(SharedPreferenceUtil.AREANEW), Area.class);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void _onError() {

                }
            });
        }
    }

    public FlowersType flowersType;
    public void getFlowers_type(){
        RxManager.getInstance().doSubscribe(new CommonModel().getFlowers(), new RxSubscriber<FlowersType>(false) {
            @Override
            protected void _onNext(FlowersType flowers) {
                flowersType = flowers;
            }

            @Override
            protected void _onError() {

            }
        });
    }


    public void loginEasemob(String userName, String password) {
        EMClient.getInstance().login(userName, password, new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                Log.e("main", "登录聊天服务器成功！");
                EMClient.getInstance().chatManager().loadAllConversations();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败！");
            }
        });
    }

    public void collect(String uid, int type, String title_id){
        RxManager.getInstance().doSubscribe(new CommonModel().add_collection(uid, type, title_id), new RxSubscriber<HttpResult>(false) {
            @Override
            protected void _onNext(HttpResult httpResult) {
                CommonUtil.myToastA(mContext, httpResult.getMsg());
            }

            @Override
            protected void _onError() {

            }
        });
    }
}